
Ext.define('proyectos.FaseForm', {
    extend: 'Ext.form.Panel',
    alias: 'widget.fasesform',
    requires: ['Ext.form.field.Text'],
    initComponent: function(){		
        Ext.apply(this, {
            activeRecord: null,
            iconCls: 'icon-fase',
            frame: true,
            defaultType: 'textfield',
            bodyPadding: 20,
            fieldDefaults: {
                anchor: '100%',
            },
            items: [{
                fieldLabel: 'Id',
                name: 'id',
                type: 'int',
                itemId: 'idFase',
                hidden: true,
            },{
                fieldLabel: 'IdProy',
                name: 'idProyectoFase',
                type: 'int',
                itemId: 'idProy',
                hidden: true,
            },{
                fieldLabel: '<b>Nombre</b>',
                name: 'nombreFase',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                itemId: 'nombre',
            }, {
                fieldLabel: '<b>Descripcion</b>',
                name: 'descripcion',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                itemId: 'descripcion',
            }],
            dockedItems: [{
				xtype: 'toolbar',
				items: [
						{
							iconCls: 'icon-add',
							itemId: 'create',
							text: '<b>Crear</b>',
							scope: this,
							hidden: true,
							handler: this.onCreate
						},{
							iconCls: 'icon-save',
							itemId: 'save',
							text: '<b>Guardar</b>',
							hidden: true,
							scope: this, 
							handler: this.onSave
						},{
						iconCls: 'icon-importar',
						itemId: 'importar',
						text: '<b>Importar</b>',
						scope: this, 
						handler: this.onImportar
					},{
						iconCls: 'icon-reordenar',
						itemId: 'reordenar',
						text: '<b>Reordenar</b>',
						scope: this, 
						handler: this.onReordenar
					},{
						iconCls: 'icon-tipoItem',
						itemId: 'tipoItem',
						//disabled: true,
						text: '<b>Asignar Tipos Item</b>',
						hidden: true,
						scope: this, 
						handler: this.onTiposItem
					},{
						iconCls: 'icon-item',
						itemId: 'item',
						//disabled: true,
						text: '<b>Items</b>',
						hidden: true,
						scope: this, 
						handler: this.onItem
					},{}]
				},{
				weight: 1,
                xtype: 'toolbar',
                dock: 'bottom',
                items: ['->',{
                    iconCls: 'icon-delete',
                    itemId: 'delete',
                    hidden: true,
                    scope: this, 
                    text: '<b>Eliminar</b>',
                    scope: this,
                    handler: this.onDelete
                },{
                    iconCls: 'icon-editar',
                    itemId: 'editar',
                    text: '<b>Editar</b>',
                    hidden: true,
                    scope: this, 
                    handler: this.onEditar
                },{
                    iconCls: 'icon-add',
                    itemId: 'nuevo',
                    text: '<b>Nuevo</b>',
                    scope: this,
                    handler: this.onNuevo
                },{
                    iconCls: 'icon-reset',
                    text: '<b>Reset</b>',
                    scope: this,
                    handler: this.onReset
                }]
            }]
        });
        this.callParent();
    },

    setActiveRecord: function(record){
        this.activeRecord = record;
        if (record) {

			if (!this.down('#editar').isDisabled()){
				this.down('#editar').show();			
			}			
            if (!this.down('#delete').isDisabled()){
				this.down('#delete').show();			
			}
			this.down('#tipoItem').show();			
			
			this.down('#item').show();			
			
            this.down('#nuevo').hide();
            this.down('#importar').hide();
            this.down('#reordenar').hide();
            this.down('#create').hide();
            this.down('#save').hide();
            this.down('#nombre').setFieldStyle('background-color: transparent; border: none;');
			this.down('#descripcion').setFieldStyle('background-color: transparent; border: none;');
            this.getForm().loadRecord(record);
        } else {

            this.down('#editar').hide();
            this.down('#delete').hide();
            this.down('#create').hide();
            this.down('#tipoItem').hide();
            this.down('#save').hide();
            this.down('#item').hide();
            this.down('#nombre').setFieldStyle('background-color: transparent; border: none;');
			this.down('#descripcion').setFieldStyle('background-color: transparent; border: none;');
            if (!this.down('#nuevo').isDisabled()){
				this.down('#nuevo').show();			
			} 
			if (!this.down('#importar').isDisabled()){
				this.down('#importar').show();			
			} 
			if (!this.down('#reordenar').isDisabled()){
				this.down('#reordenar').show();			
			} 
			
            this.down('#nombre').setValue("");
            this.down('#descripcion').setValue("");
            this.down('#idFase').setValue("");
            
        }
    },
    
	onEditar: function(){
		this.down('#tipoItem').hide();
        this.down('#item').hide();
        this.down('#editar').hide();
		this.down('#delete').hide();
		this.down('#save').show();
		this.down('#nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#descripcion').setFieldStyle('background-color: #fff; border: 1px dotted;');
		
    },
    
	onNuevo: function(){
		this.down('#importar').hide();
		this.down('#reordenar').hide();
		this.down('#nuevo').hide();
		this.down('#create').show();
		this.down('#nombre').setValue("");
        this.down('#descripcion').setValue("");
        this.down('#idFase').setValue("");
		this.down('#nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#descripcion').setFieldStyle('background-color: #fff; border: 1px dotted;');
		
    },
    
    onCreate: function(){
        var form = this.getForm();
                    form.submit({                        
                        url: 'crear_fase',
                        params: {	submit: 'submit'	},
						method: "post",
                       success: function(form, action) {
								this.down('#nombre').setValue("");
								this.down('#descripcion').setValue("");
								this.down('#idFase').setValue("");
							    Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Fase creada satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',
								});
							this.fireEvent('refresh');							                       
                        },
                        scope: this,                        
                        failure: function(form, action) {									
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido crear la fase', //<- El mensaje  
										icon: 'ext-mb-error',
									});
                        }
                    });
    },
    
    onSave: function(){
        var form = this.getForm();
                    form.submit({                        
                        url: 'modificar_fase',
                        params: {	submit: 'submit'	},
						method: "post",
                       success: function(form, action) {
								this.down('#nombre').setValue("");
								this.down('#descripcion').setValue("");
								this.down('#idFase').setValue("");
							    Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Fase modificada satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',
								});
							this.fireEvent('refresh');
							this.onReset();								                       
                        },
                        scope: this,                        
                        failure: function(form, action) {									
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido modificar la fase', //<- El mensaje  
										icon: 'ext-mb-error',
									});
                        }
                    });
    },
        
    onDelete: function(){ 
        var form = this.getForm();
                    form.submit({                        
                        url: 'eliminar_fase',
                        params: {	submit: 'submit'	},
						method: "post",
                       success: function(form, action) {
								this.down('#nombre').setValue("");
								this.down('#descripcion').setValue("");
								this.down('#idFase').setValue("");
							    Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Fase eliminada satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',
								});
							this.fireEvent('refresh');
							this.onReset();							                       
                        },
                        scope: this,                        
                        failure: function(form, action) {									
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido eliminar la fase', //<- El mensaje  
										icon: 'ext-mb-error',
									});
                        }
                    });
    },  
     
	onImportar: function(){
			//Ext.Msg.alert(this.down('#idProy').getValue());
			var form = this.getForm();
			this.fireEvent('ddImportar', form.getValues(true));            
	}, 

	onReordenar: function(){
			var form = this.getForm();
			this.fireEvent('ddReordenar', form.getValues(true));            
	}, 	
	
	onTiposItem: function(){
			var form = this.getForm();
			this.fireEvent('ddTiposItem', form.getValues(true));            
	}, 
	
	onItem: function(){
			var form = this.getForm();
			this.fireEvent('ddItem', form.getValues(true));            
	}, 	    

    onReset: function(){
        this.setActiveRecord(null);
        //this.getForm().reset();
        this.fireEvent('refresh');
    },

    
});
